package com.example.demo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.example.demo.bean.dto.MyFriendsAddInfoDTO;
import com.example.demo.bean.vo.FriendReviewVO;
import com.example.demo.entity.UsersFriend;
import com.example.demo.result.Result;
import com.example.demo.service.IUsersFriendService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 好友关系 前端控制器
 * </p>
 *
 * @author 言浠
 * @since 2024-03-28
 */
@Api(tags = "好友关系接口")
@RestController
@RequestMapping("/users-friend")
public class UsersFriendController {

    @Resource
    private IUsersFriendService usersFriendService;

    @SaCheckLogin
    @GetMapping("/getMyFriends")
    @ApiOperation(value = "获取好友列表")
    @ApiOperationSupport(author = "言浠")
    public Result<List<MyFriendsAddInfoDTO>> addFriend(String userId, String friendId) {
        //通过token获取自己的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        List<MyFriendsAddInfoDTO> myFriends = usersFriendService.getFriendByFriendId(loginIdAsInt);
        return Result.success(myFriends).msg("好友列表");
    }
    /**
     * 好友审核
     */
    @SaCheckLogin
    @PostMapping("/friendReview")
    @ApiOperation(value = "好友审核")
    @ApiOperationSupport(author = "言浠")
    public Result<List<MyFriendsAddInfoDTO>> checkFriend(@RequestBody FriendReviewVO friendReviewVO) {
        //我同意他的好友请求
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        UsersFriend usersFriend = new UsersFriend();
        BeanUtil.copyProperties(friendReviewVO,usersFriend);
        usersFriend.setFriendId(loginIdAsInt);
        usersFriendService.updateById(usersFriend);
        //我同意添加对方后，我也应该是对方的好友
        UsersFriend usersFriend1 = new UsersFriend();
        usersFriend1.setUserId(loginIdAsInt);
        //user_friend 的id查询这个人的user_id
        Integer userId = usersFriendService.getById(friendReviewVO.getId()).getUserId();
        usersFriend1.setFriendId(userId);
        usersFriend1.setStatus(1);
        usersFriendService.save(usersFriend1);
        return Result.success().msg("操作成功");
    }

    /**
     * 查询谁要添加我为好友
     */
    @SaCheckLogin
    @GetMapping("/getMyFriendsAddInfo")
    @ApiOperation(value = "查询谁要添加我为好友")
    @ApiOperationSupport(author = "言浠")
    public Result<List<MyFriendsAddInfoDTO>> getMyFriendsAddInfo() {
        //通过token获取自己的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        List<MyFriendsAddInfoDTO> myFriends = usersFriendService.getFriendByUserId(loginIdAsInt);
        return Result.success(myFriends).msg("查询成功");
    }

    /**
     * 添加某人为好友，传入被添加人id
     */
    @SaCheckLogin
    @PostMapping("/add/{id}")
    @ApiOperation(value = "添加某人为好友")
    @ApiOperationSupport(author = "言浠")
    @ApiImplicitParams({@ApiImplicitParam(name = "friendId",value = "好友id",required = true)})
    public Result<List<MyFriendsAddInfoDTO>> addFriend(@PathVariable Integer id) {
        //通过token获取自己的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();

        if(loginIdAsInt == id){
            return Result.error().msg("不能添加自己为好友");
        }

        UsersFriend usersFriend = new UsersFriend();
        usersFriend.setUserId(loginIdAsInt);
        usersFriend.setFriendId(id);
        boolean b = usersFriendService.addFriend(usersFriend);
        if(!b){
            return Result.error().msg("等待同意或你被拉黑或已是好友");
        }
        return Result.success().msg("添加成功");
   }
}
